$(function () {
    //获取id
    var id = tools.queryString().id || 1;
    //获取活动数据
    $.ajax({
        type: "get",
        // url: domain.test + "/getactivitydetail",
        url: domain.online + "/api/activity",
        data: { id: id },
        async: false,
        success: function (response) {
            // if (response.data.rtnCode === 200) {
            if(response.rtnCode===200) {
                // var activityDetail = response.data.data;
                var activityDetail = response.data;
                window.cuteSecond = activityDetail.second;
                $(".activityBox").html(template("activityBox", { activityDetail: activityDetail }));
            }
        }
    });
    //倒计时
    if(window.cuteSecond&&window.cuteSecond>1000){
        var timer = setInterval(function () {
            cuteSecond -= 1000;
            if (cuteSecond === 0) {
                clearInterval(timer);
                $(".acText").html("距离活动结束");
                var actime = 1800000;
                var actimer = setInterval(function() {
                    actime--;
                    if(actime===0) {
                        clearInterval(actimer);
                        $(".activityTime_c3").html("活动已结束");
                    }
                    var hour = Math.floor(actime / 1000 / 3600);
                    var minute = Math.floor(actime / 1000 / 60 % 60);
                    var second = Math.floor(actime / 1000 % 60);
                    hour = hour >= 10 ? hour : "0" + hour;
                    minute = minute >= 10 ? minute : "0" + minute;
                    second = second >= 10 ? second : "0" + second;
                    $(".hour").html(hour);
                    $(".minute").html(minute);
                    $(".second").html(second);
                });
            }
            var hour = Math.floor(cuteSecond / 1000 / 3600);
            var minute = Math.floor(cuteSecond / 1000 / 60 % 60);
            var second = Math.floor(cuteSecond / 1000 % 60);
            hour = hour >= 10 ? hour : "0" + hour;
            minute = minute >= 10 ? minute : "0" + minute;
            second = second >= 10 ? second : "0" + second;
            $(".hour").html(hour);
            $(".minute").html(minute);
            $(".second").html(second);
        }, 1000);
    }
    //获取活动评论
    var acPageNum = 1;
    window.acCommentList = [];
    function getActivityComment(id, acPageNum, CommentList) {
        $.ajax({
            type: "get",
            // url: domain.test + "/getactivitycomments",
            url:domain.online + "/api/activity/review",
            data: { id: id, pageNum: acPageNum },
            success: function (data) {
                // if (data.data.rtnCode === 200) {
                if(data.rtnCode === 200) {
                    // var acCommentList = data.data.data;
                    var acCommentList = data.data;
                    window.acCommentList = CommentList.concat(acCommentList.list);
                    window.acMore = acCommentList.more;
                    template.helper("acMore", function () {
                        return window.acMore;
                    });
                    $(".comments").html(template("comments", { commentList: window.acCommentList }));
                }
            }
        });
    }
    getActivityComment(id, acPageNum, window.acCommentList);
    $(".comments").on("click", ".moreCommentsBtn", function () {
        if (window.acMore) {
            acPageNum++;
            getActivityComment(id, acPageNum, window.acCommentList);
        } else {
            $(this).html("没有更多评论了");
            return;
        }
    });
    //查看详情
    $(".activityBox").on("click", ".productItem", function () {
        var pID = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pID;
    });
    //订单列表滚动
    var $this = $(".orderList");
    var scrollTimer;
    $this.hover(function () {
        clearInterval(scrollTimer);
    }, function () {
        scrollTimer = setInterval(function () {
            scrollNews($this);
        }, 2000);
    }).trigger("mouseout");
    function scrollNews(obj) {
        var $self = obj.find(".itemBox:first");
        var lineHeight = $self.find(".orderItem:first").height();
        $self.animate({ "margin-top": -lineHeight + "px" }, 350, function () {
            $self.css({ "margin-top": "0px" }).find(".orderItem:first").appendTo($self);
        })
    }
});